package be.kul.osgi.ds.MesIDAgent;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;

import be.kul.osgi.ds.comminterface.CommunicationInterface;
import be.kul.osgi.ds.comminterface.MyEvent;
import be.kul.osgi.ds.comminterface.MyEventListener;
import be.kul.osgi.ds.messageservice.MessageObject;
import be.kul.osgi.ds.messageservice.MessageService;
import be.kul.osgi.ds.xmlstreamservice.XMLStreamService;


public class MessageIDAgent implements MyEventListener{

	private CommunicationInterface commservice;
	private MessageService messageservice;
	String name = "mesIDAgent";
	double ID;
	public XMLStreamService XMLStreamservice;
	
	//this is executed after all services are loaded
	public void activate() {
		System.out.println("Activate in myAgent executed");
		socket_init(4567, "224.0.0.123");
		
		//commservice.send("test", "224.0.0.123");
		
	}
	
	// this is activated before services are loaded. 
	// This is needed to start the component, not for activating it.
	public MessageIDAgent(){
		System.out.println("MesIDAgent executed");
		this.ID = 0;
		
		/*final ContractInitiator ci = new ContractInitiator();
		ci.fsm.on();
		ci.fsm.on();
		ci.fsm.ack();
		ci.fsm.ack();
		
		int numberOfMillisecondsInTheFuture = 3000; // 10 sec
		Date timeToRun = new Date(System.currentTimeMillis()+numberOfMillisecondsInTheFuture);
		Timer timer = new Timer();

		timer.schedule(new TimerTask() {
		        public void run() {
		           System.out.println("@MyAgent - TimerTest - ackmessage");
		           ci.fsm.ack();
		        }
		    }, timeToRun);
		*/
	}
	
	
	public MessageIDAgent(String n){
		System.out.println("MesIDAgent executed");
		this.name = n;
		this.ID = 0;
	}
	
	
	//init the socket
	
		public void socket_init(int port, String group)
		{
			this.commservice.init(port);
			this.commservice.addToGroup(group);
			//this.commservice.initserver();
			this.commservice.receive();
			this.commservice.addMyEventListener(this);
		}
	
	/*
	 * COMMUNICATION INTERFACE - UDP in this case
	 */
	
	public synchronized void setComm(CommunicationInterface commservice) {
		System.out.println("Communicationcomponent was set. Thank you DS!");
		this.commservice = commservice;
	}

	// Method will be used by DS to unset the quote service
	public synchronized void unsetComm(CommunicationInterface commservice) {
		System.out.println("Communicationcomponent was unset. Why did you do this to me?");
		if (this.commservice == commservice) {
			this.commservice = null;
		}
	}
	
	/*
	 * MESSAGING INTERFACE 
	 */
	
	
	
	public synchronized void setMessage(MessageService messageservice) {
		System.out.println("Messagecomponent was set. Thank you DS!");
		this.messageservice = messageservice;
	}

	public synchronized void unsetMessage(MessageService messageservice) {
		System.out.println("Messagecomponent was unset. Why did you do this to me?");
		if (this.messageservice == messageservice) {
			this.messageservice = null;
		}
	}
	
	public MessageService getMessageService(){
		return this.messageservice;
	}

	
	/*
	 * XMLSTREAMSERVICE
	 */
	public void setXMLStreamService(XMLStreamService service){
		System.out.println("Messagecomponent was set. Thank you DS!");
		this.XMLStreamservice = service;		
	}
	
	public void unsetXMLStreamService(XMLStreamService service){
		System.out.println("XMLServicecomponent was unset. Why did you do this to me?");
		if (this.XMLStreamservice == service) {
			this.XMLStreamservice = null;
		}		
	}
	
	public XMLStreamService getXMLStreamService()
	{
		return XMLStreamservice;
	}
	
	@Override
	public void messageReceived(MyEvent event) {
		// TODO Auto-generated method stub
		MessageObject mo = new MessageObject();
		mo = XMLStreamservice.readClass(event.mes, MessageObject.class);
		//MessageObject returnMessage = new MessageObject();
		System.out.println("sender is : " + mo.getSender() + " with port : " + event.sendersocket);
		System.out.println("MessageIDAgent, received something : " + event.mes);
		if ((mo.getReceiver().equals(this.name) || mo.getReceiver().equals("multi")) && mo.getSender() != this.name) {
			if (mo.getPerformative() == MessageObject.GETID){
				//got message
				
				System.out.println("MessageIDAgent, received request!");
				//make message
				MessageObject returnMessage = new MessageObject(MessageObject.SENDID);
				returnMessage.setContent((new Double(ID++)).toString());
				returnMessage.setReceiver(mo.getSender());
				//serialize message
				String s = XMLStreamservice.toXML(returnMessage);
				//send message
				//this.commservice.send(s,sender);
				this.commservice.sendmulticast(s);
				//this.commservice.sendmulticast(s);
			}
		}
	}
	
}
